const app = getApp()
// const user = wx.getStorageSync("user");
var user_token = wx.getStorageSync('user_token');
const base_url = app.globalData.base_url;

const date = new Date()
const years = []
const months = []
const days = []
const util = require('../../utils/util.js');
for (let i = 1900; i <= date.getFullYear(); i++) {
  years.push(i)
}

for (let i = 1; i <= 12; i++) {
  months.push(i)
}

for (let i = 1; i <= 31; i++) {
  days.push(i)
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    item1: ["京", "沪", "浙", "苏", "粤", "鲁", "晋", "冀",
      "豫", "川", "渝", "辽", "吉", "黑", "皖", "鄂",
      "津", "贵", "云", "桂", "琼", "青", "新", "藏",
      "蒙", "宁", "甘", "陕", "闽", "赣", "湘"],
    item2: ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
      "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P",
      "A", "S", "D", "F", "G", "H", "J", "K", "L",
      "Z", "X", "C", "V", "B", "N", "M"],
    hidden1: true,
    hidden2: true,
    carFlag: false,
    carinfo: {},
    sex: '1',
    pickerFlag: false,
    years: years,
    birthday: '',
    year: date.getFullYear(),
    months: months,
    month: date.getMonth() + 1,
    days: days,
    day: date.getDate(),
    value: [9999, 1, 1],
    formData: {},
    selectSex: [
      { name: '男', value: '1' },
      { name: '女', value: '0' },
    ],
    real_name: '',
    company: '',
    company_address: '',
    company_introduction: '',
    constellation: '',
    position: '',
    wx_number: '',
    index: 0,
    data_car_no: 1,
  },
  onLoad: function (options) {
    wx.request({
      url: base_url + '/CarUsers/getMyCarInfo',
      method: 'POST',
      data: {
        'user_token': JSON.parse(wx.getStorageSync("user")).user_token
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        console.log(res)
        if (res.data.success == '1') {
          this.setData({
            carinfo: res.data.data,
            'formData.birthday': res.data.data.brithday
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          }),
            wx.navigateBack({
              delta: 1
            })
        }
      }
    })
  },
  confirm: function () {
    let data = this.data.formData;
    this.setData({ 'formData.user_token': JSON.parse(wx.getStorageSync("user")).user_token })
    wx.request({
      url: base_url + '/CarUsers/setUserInfo',
      method: 'POST',
      data: data,
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        if (res.data.success == '1') {
          wx.redirectTo({
            url: '../myInfo/index'
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
        }

      }
    })
  },

  selSex: function (e) {
    this.setData({ 'formData.sex': e.currentTarget.dataset.sex })
  },
  selBirth: function () {//打开时间选择器
    this.setData({ pickerFlag: !this.data.pickerFlag })
  },
  clear: function () {//关闭选择器
    this.setData({ pickerFlag: false })
  },
  saveBirth: function () {//提交生日
    this.setData({
      'formData.birthday': util.zero(this.data.year) + '-' + util.zero(this.data.month) + '-' + util.zero(this.data.day),
      pickerFlag: false
    })
  },
  getData(e) {
    let vtype = e.currentTarget.dataset.vtype;
    let label = 'formData.' + vtype;
    this.setData({
      [label]: e.detail.value
    })
  },
  bindChange: function (e) {//时间选择器
    const val = e.detail.value
    this.setData({
      year: this.data.years[val[0]],
      month: this.data.months[val[1]],
      day: this.data.days[val[2]]
    })
  },
  selCarStr: function (e) {//打开省选择器
    this.setData({
      carFlag: !this.data.carFlag,
      data_car_no: e.currentTarget.dataset.no
    })
    var that = this;
    that.setData({
      hidden1: false,
      hidden2: true
    })

  },
  selCarNo: function (e) {//打开车牌选择器
    this.setData({
      carFlag: !this.data.carFlag,
      data_car_no: e.currentTarget.dataset.no
    })
    var that = this;

    that.setData({
      hidden1: true,
      hidden2: false
    })
  },
  //车牌输入获取焦点  
  d1: function () {
    var that = this;
    if (that.data.carNo == '') {
      that.setData({
        hidden1: false,
        hidden2: true
      })
    } else {
      that.setData({
        hidden1: true,
        hidden2: false
      })
    }

  },
  //车牌输入失去焦点  
  d2: function () {
    var that = this;
    that.setData({
      hidden1: true,
      hidden2: true
    })
  },
  //获取车牌省份  
  sheng: function (e) {
    var that = this;
    var carno = e.currentTarget.dataset.no;
    var car_str = e.currentTarget.dataset.sh;
    wx.request({
      url: base_url + '/CarUsers/editCarNo',
      method: 'POST',
      data: {
        user_token: JSON.parse(wx.getStorageSync("user")).user_token,
        id: that.data.carinfo.car[carno - 1].id,
        car_str: car_str
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        if (res.data.success == '1') {
          that.data.carinfo.car[carno - 1].car_str = car_str
          that.setData({
            carinfo: that.data.carinfo,
            hidden1: true,
            hidden2: false
          })
        }
      }
    })
  },
  //获取车牌号码  
  other: function (e) {
    var that = this;
    console.log(e.currentTarget.dataset.ot);
    var car = e.currentTarget.dataset.no;
    if (that.data.carinfo.car[car - 1].car_number.length < 7) {
      var carNo = that.data.carinfo.car[car - 1].car_number + e.currentTarget.dataset.ot;
      that.data.carinfo.car[car - 1].car_number = carNo
      that.setData({
        carinfo: that.data.carinfo
      })
    }

  },
  //回删车牌  
  del: function (e) {
    var car = e.currentTarget.dataset.no;
    var that = this;
    var ss = that.data.carinfo.car[car - 1].car_number;
    var s = ss.split('');
   
    var s = s.join('').slice(0, -1);
    that.data.carinfo.car[car - 1].car_number = s
    that.setData({
      carinfo: that.data.carinfo
    })

  },
  //确认输入  
  ok: function (e) {
    var that = this;
    var car = e.currentTarget.dataset.no;

    var re = new RegExp('^[A-Za-z0-9]{7}|[A-Za-z0-9]{6}$');
    if (!re.exec(that.data.carinfo.car[car - 1].car_number)) {
      wx.showToast({
        title: '车牌号格式不正确',
        icon: 'none',
        duration: 2000
      })
      return;
    }

    wx.request({
      url: base_url + '/CarUsers/editCarNo',
      method: 'POST',
      data: {
        user_token: JSON.parse(wx.getStorageSync("user")).user_token,
        id: that.data.carinfo.car[car - 1].id,
        car_number: that.data.carinfo.car[car - 1].car_number
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        if (res.data.success == '1') {
          that.setData({
            hidden1: true,
            hidden2: true
          })
        }
      }
    })
  },
  delCar: function (e) {
    var that = this;
    var car = e.currentTarget.dataset.car;
    var no = e.currentTarget.dataset.no;
    wx.showModal({
      title: '提示',
      content: '确认删除' + car + '?',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: base_url + '/CarUsers/delCar',
            method: 'POST',
            data: {
              user_token: JSON.parse(wx.getStorageSync("user")).user_token,
              id: that.data.carinfo.car[no - 1].id
            },
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            success: res => {
              if (res.data.success == '1') {
                that.data.carinfo.car.splice(no - 1, 1);
                that.setData({
                  carinfo: that.data.carinfo
                })
              }
            }
          })
        }
      }
    })
  }


})
